using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace WaterDemo_XNA
{
	public class XNAGame : Microsoft.Xna.Framework.Game
	{
		GraphicsDeviceManager graphics;
		SpriteBatch spriteBatch;
		SpriteFont spriteFont;
		WaterDemo.Game game;
		bool loaded;

		public XNAGame()
		{
			graphics = new GraphicsDeviceManager(this);
			graphics.PreferredBackBufferWidth = 512;
			graphics.PreferredBackBufferHeight = 512;
			graphics.PreferredDepthStencilFormat = DepthFormat.Depth24Stencil8;
			graphics.SynchronizeWithVerticalRetrace = true;

			Content.RootDirectory = "Content";
		}

		protected override void LoadContent()
		{
			try
			{
				game = new WaterDemo.Game(this, graphics);
				spriteBatch = new SpriteBatch(GraphicsDevice);
				spriteFont = Content.Load<SpriteFont>("MainFont");
				loaded = true;
			}
			catch (Exception e)
			{
				Reign.Core.Message.Show(e.Message);
				Exit();
			}
		}

		protected override void UnloadContent()
		{
			if (game != null)
			{
				loaded = false;
				game.Dispose();
				game = null;
			}
		}

		protected override void Update(GameTime gameTime)
		{
			if (loaded) game.Update();
			base.Update(gameTime);
		}

		protected override void Draw(GameTime gameTime)
		{
			if (loaded)
			{
				game.Render();

				#if !DEBUG
				spriteBatch.Begin();
				spriteBatch.DrawString(spriteFont, game.FPS.ToString(), new Vector2(), Color.Green);
				spriteBatch.End();
				#endif
			}

			base.Draw(gameTime);
		}
	}
}
